package org.example.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.constant.ManagerConstants;
import org.example.domain.SysRoleMenu;
import org.example.mapper.SysRoleMenuMapper;
import org.example.service.SysRoleMenuService;
import org.example.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.domain.SysRole;
import org.example.mapper.SysRoleMapper;
import org.example.service.SysRoleService;
import org.springframework.transaction.annotation.Transactional;

@Service
@CacheConfig(cacheNames = "org.example.service.Impl.SysRoleServiceImpl")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    @Cacheable(key = ManagerConstants.SYS_ALL_ROLE_KEY)
    public List<SysRole> querySysRoleList() {
        return sysRoleMapper.selectList(new LambdaQueryWrapper<SysRole>()
                .orderByDesc(SysRole::getCreateTime)
        );
    }

    @Override
    @CacheEvict(key = ManagerConstants.SYS_ALL_ROLE_KEY)
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveSysRole(SysRole sysRole) {

        sysRole.setCreateTime(new Date());
        sysRole.setCreateUserId(AuthUtils.getLoginUserId());
        int i= sysRoleMapper.insert(sysRole);
        if (i> 0) {
            //获取角色id
            Long roleId = sysRole.getRoleId();
            //新增角色与权限的关系记录
            //获取角色对应的权限id集合
            List<Long> menuIdList = sysRole.getMenuIdList();

            List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(menuIdList) && menuIdList.size()!=0){
                //循环遍历权限id集合
                menuIdList.forEach(menuId ->{
                    //创建角色与权限关系记录
                    SysRoleMenu sysRoleMenu = new SysRoleMenu();
                    sysRoleMenu.setRoleId(roleId);
                    sysRoleMenu.setMenuId(menuId);

                    sysRoleMenuList.add(sysRoleMenu);
                });
                sysRoleMenuService.saveBatch(sysRoleMenuList);
            }
        }
        return i> 0;
    }

    @Override
    public SysRole querySysRoleInfoByRoleId(Long roleId) {
        // 根据角色标识查询角色详情
        SysRole sysRole = sysRoleMapper.selectById( roleId);
        // 根据角色标识查询角色与权限关系集合
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId)
        );
        // 判断是否有值
        if (CollectionUtil.isNotEmpty(sysRoleMenuList) && sysRoleMenuList.size() != 0) {
            // 说明角色与权限关系有值
            // 从角色与权限关系集合中获取权限id集合
            List<Long> menuIdList = sysRoleMenuList.stream().map( SysRoleMenu::getMenuId).collect(Collectors.toList());
            sysRole.setMenuIdList(menuIdList);
        }
        return sysRole;
    }

    @Override
    @CacheEvict(key = ManagerConstants.SYS_ALL_ROLE_KEY)
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifySysRole(SysRole sysRole) {
        // 获取角色标识
        Long roleId = sysRole.getRoleId();
        // 删除角色原有的权限集合
        sysRoleMenuMapper.delete( new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId)
        );

        List<Long> menuIdList = sysRole.getMenuIdList();

        List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(menuIdList) && menuIdList.size()!=0){
            //循环遍历权限id集合
            menuIdList.forEach(menuId ->{
                //创建角色与权限关系记录
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenu.setMenuId(menuId);

                sysRoleMenuList.add(sysRoleMenu);
            });
            sysRoleMenuService.saveBatch(sysRoleMenuList);
        }

        return sysRoleMapper.updateById(sysRole) > 0;
    }

    @Override
    @CacheEvict(key = ManagerConstants.SYS_ALL_ROLE_KEY)
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeById(List<Long> roleIdList) {
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                .in(SysRoleMenu::getRoleId, roleIdList));
        return sysRoleMapper.deleteBatchIds(roleIdList) > 0;
    }


}
